<?php
namespace app\blog\model;
use think\db;
use think\Model;
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
class CountsModel extends Model
{

    public function __construct($data = [])
    {
        parent::__construct($data);
        $id=cmf_get_current_admin_id();
        $this->id=$id;
    }

    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $cause['id']=$id;
        $user=DB::name('user')->where($cause)->find();
        if($user['user_type']==1)
        {
            $where='';
            $session=DB::name('shop')->where($where)->select();
        }else{
            $where['re_guid']=$user['shop_guid'];
            $session=DB::name('shop')->where($where)->select()->toArray();
        }
        return $session;
    }
    public function index($cause)
    {
        /*条件编写*/
        $session=$this->session();
        if($session)
        {
            $where="";
            if($this->id==1)
            {
                $where='1=1';
            }else{
                $where ="  shop_id = '".$session[0]['re_guid']."'";
            }
            if(isset($cause['goods_id']))
            {
                if($cause['goods_id'])
                {
                    $where .=" and goods_id =".$cause['goods_id'];
                }
            }else{
                $cause['order_num']='';
            }
            $why="";
            if(isset($cause['goods_name']))
            {
                if($cause['goods_name'])
                {
                    $why['goods_name']=array('like',"%".$cause['goods_name']."%");
                }
            }else{
                $cause['goods_name']='';
            }
            if(isset($cause['start_time']) && isset($cause['end_time']))
            {
                if(!empty($cause['start_time']))
                {
                    $start=strtotime($cause['start_time']);
                    if(!empty($cause['end_time']))
                    {
                        $end=strtotime($cause['end_time']);
                    }else{
                        $end=$start+86400;
                    }
                    $where.=' and w_time  >='.$start;
                    $where.=' and w_time  <='.$end;
                }
            }else{
                $cause['start_time']='';
                $cause['end_time']='';
            }
            if(isset($cause['order_by']))
            {
                if($cause['order_by']==1)
                {
                    $data=DB::name('order_detail')->alias('or')->whereOr($why)->where($where)->group('goods_id')->field('or.*,sum(money) as total_money,count(num) as number')->order('sum(money) asc')->paginate(10);
                }else{
                    $data=DB::name('order_detail')->alias('or')->whereOr($why)->where($where)->group('goods_id')->field('or.*,sum(money) as total_money,count(num)  as number')->order('sum(money) desc')->paginate(10);
                }
            }else{
                $data=DB::name('order_detail')->alias('or')->whereOr($why)->where($where)->group('goods_id')->field('or.*,sum(money) as total_money,count(num)  as number')->paginate(10);
            }
            /*对数据进行封装*/
            if($data->toArray())
            {

                foreach ($data as $k=>$v)
                {
                    $date=array();
                    $date=$v;
                    if($date['stemeal']==1)
                    {
                        $date['stemeal_name']='单品';
                    }else{
                        $date['stemeal_name']='套餐';
                    }
                    $data->offsetSet($k,$date);

                }
            }
            /*分页保存条件*/
            $data->appends($cause);
            $array['data']=$data;
            $array['page']=$data->render();
        }
        else{
            $array['data']="";
            $array['page']="";
        }
        return $array;
    }

}